package com.gdk.ksoft.ui.works.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.utils.ColorUtils;
import com.gdk.ksoft.data.common.SystemData;
import com.gdk.ksoft.ui.works.models.FworkCustomerModel;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Mrper on 2014/11/25.
 * 首页 --- 客户数据适配器
 */
public class FworkCustomerAdapter extends BaseAdapter {

    public HashMap<String,Integer> colorMap = new HashMap<String,Integer>();
    private Context context;
    private List<FworkCustomerModel> lst;
    public FworkCustomerAdapter(Context context,List<FworkCustomerModel> lst){
        this.context = context;
        this.lst = lst;
    }

    @Override
    public int getCount() {
        return this.lst == null ?0:this.lst.size();
    }

    @Override
    public Object getItem(int position) {
        return this.lst.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final FworkCustomerModel item = this.lst.get(position);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_fwork_lstcustomer,null);
            holder.txtIcon =(TextView)convertView.findViewById(R.id.item_fwork_lstcustomer_txtIcon);
            holder.txtCustomerName = (TextView)convertView.findViewById(R.id.item_fwork_lstcustomer_txtCustomerName);
            holder.txtContactor = (TextView)convertView.findViewById(R.id.item_fwork_lstcustomer_txtContactor);
            holder.txtReceiver = (TextView)convertView.findViewById(R.id.item_fwork_lstcustomer_txtContacts);
            holder.txtReceiverAddress = (TextView)convertView.findViewById(R.id.item_fwork_lstcustomer_txtAddress);
            holder.txtDistance = (TextView)convertView.findViewById(R.id.item_fwork_lstcustomer_txtDistace);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        int color = 0;//默认颜色值
        if (!colorMap.containsKey(item.customerType)) {
            color = ColorUtils.GetColor();
            colorMap.put(item.customerType, color);
        }else{
            color = colorMap.get(item.customerType);
        }
        holder.txtIcon.setBackgroundDrawable(new ColorDrawable(color));
        holder.txtIcon.setText(item.customerType);
        holder.txtIcon.setTextColor(Color.rgb(Math.abs(Color.red(color)-255),Math.abs(Color.green(color)-255),Math.abs(Color.blue(color)-255)));
        holder.txtCustomerName.setText(item.customerName);
        holder.txtContactor.setText(item.contactorName+" "+item.contactorPhone);
        holder.txtReceiver.setText(item.receiverName+" "+item.receiverPhone);
        holder.txtReceiverAddress.setText("地址："+item.receiverAddress);
        try {
            //计算距离信息
            if (!TextUtils.isEmpty(item.latLng.trim()) && SystemData.currentLocation != null) {
                String[] latlng = item.latLng.split(",");
                holder.txtDistance.setText(
                        FormatUtils.exactDistance(
                                new LatLng(SystemData.currentLocation.getLatitude(),SystemData.currentLocation.getLongitude()),
                                new LatLng(Double.parseDouble(latlng[0]),Double.parseDouble(latlng[1]))
                        )
                );
            } else {
                holder.txtDistance.setText("");
            }
        }catch(Exception e){
            holder.txtDistance.setText("");
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder{
        public TextView txtIcon;
        public TextView txtCustomerName;
        public TextView txtContactor;
        public TextView txtReceiver;
        public TextView txtReceiverAddress;
        public TextView txtDistance;
    }

}
